<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"singleplus.gitee.io","root":"/blog/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="&amp;emsp;&amp;emsp;昨天刷Leetcode遇见的新的数据结构，特此记录以供后续学习。 lowbit()函数的意义&amp;emsp;&amp;emsp;lowbit()函数是求一个十进制数的二进制表示第一个1与后面的一串0构成的数的大小，例如我想要求 $low(12)$的值，那么 $(12)_{10}$的二进制表示为 $(1100)_2$，那么lowbit(12)的结果就是$(100)_2$也就是数4。那么我">
<meta property="og:type" content="article">
<meta property="og:title" content="树状数组">
<meta property="og:url" content="https://singleplus.gitee.io/blog/2020/07/15/%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="&amp;emsp;&amp;emsp;昨天刷Leetcode遇见的新的数据结构，特此记录以供后续学习。 lowbit()函数的意义&amp;emsp;&amp;emsp;lowbit()函数是求一个十进制数的二进制表示第一个1与后面的一串0构成的数的大小，例如我想要求 $low(12)$的值，那么 $(12)_{10}$的二进制表示为 $(1100)_2$，那么lowbit(12)的结果就是$(100)_2$也就是数4。那么我">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/SinglePlus/Image/raw/master/imgs/20200712161435.png">
<meta property="article:published_time" content="2020-07-15T13:40:01.000Z">
<meta property="article:modified_time" content="2020-07-24T09:01:36.078Z">
<meta property="article:author" content="Cheng">
<meta property="article:tag" content="Leetcode">
<meta property="article:tag" content="前缀和">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/SinglePlus/Image/raw/master/imgs/20200712161435.png">

<link rel="canonical" href="https://singleplus.gitee.io/blog/2020/07/15/%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>树状数组 | Hexo</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Hexo</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://singleplus.gitee.io/blog/2020/07/15/%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="Cheng">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          树状数组
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-07-15 21:40:01" itemprop="dateCreated datePublished" datetime="2020-07-15T21:40:01+08:00">2020-07-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-07-24 17:01:36" itemprop="dateModified" datetime="2020-07-24T17:01:36+08:00">2020-07-24</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" itemprop="url" rel="index"><span itemprop="name">算法与数据结构</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>&emsp;&emsp;昨天刷Leetcode遇见的新的数据结构，特此记录以供后续学习。</p>
<h3 id="lowbit-函数的意义"><a href="#lowbit-函数的意义" class="headerlink" title="lowbit()函数的意义"></a><code>lowbit()</code>函数的意义</h3><p>&emsp;&emsp;<code>lowbit()</code>函数是求一个十进制数的二进制表示第一个1与后面的一串0构成的数的大小，例如我想要求 <small>$low(12)$</small>的值，那么 <small>$(12)_{10}$</small>的二进制表示为 <small>$(1100)_2$</small>，那么<code>lowbit(12)</code>的结果就是<small>$(100)_2$</small>也就是数<code>4</code>。那么我们在实际运算的时候怎么求解呢？这时候就需要用到位运算，我们只需要将该数字取反后加一，然后按位与即可得到。而在实际计算机实际表示中，相反数是利用补码存储，而数字按位取反后加一正是补码的运算，所以推出，至于这个函数有什么作用，请接着往下看。</p>
<script type="math/tex; mode=display">
lowbit(x) = x \&-x</script><h3 id="树状数组详解"><a href="#树状数组详解" class="headerlink" title="树状数组详解"></a>树状数组详解</h3><p>&emsp;&emsp;树状数组说白了，就是数据结构建立在数组上的，具体的操作请看下文</p>
<div align=center>
<img src="https://gitee.com/SinglePlus/Image/raw/master/imgs/20200712161435.png">
</div>

<p>假设我们操作的原数组是<code>A</code>,<code>C</code>是操作完成后的数组，那么<code>A</code>和<code>C</code>的对应关系为</p>
<ul>
<li><code>C[1] = A[1]</code></li>
<li><code>C[2] = A[1] + A[2]</code></li>
<li><code>C[3] = A[3]</code></li>
<li><code>C[4] = A[1] + A[2] + A[3] + A[4]</code></li>
<li><code>C[5] = A[5]</code></li>
<li><code>C[6] = A[5] + A[6]</code></li>
<li><code>C[7] = A[7]</code></li>
<li><code>C[8] = A[1] + A[2] + A[3] + A[4] + A[5] + A[6] + A[7] + A[8]</code></li>
</ul>
<p>&emsp;&emsp;那这个数据结构有什么作用呢，计算前缀和或者区间和，比如当前我们要计算<code>sum[7]</code>时，这是就需要用到前文提到的<code>lowbit()</code>函数，有<code>lowbit(7)=6</code>，<code>lowbit(6)=4</code>，<code>lowbit(4)=0</code>也就是说我们只需要计算<code>C[7]+C[6]+C[4]</code>就可以得到前七个数的和，如果我们要计算<code>sum[3,7]</code>,只需要<code>sum[7]-sum[2]</code>就可以得到区间和。</p>
<h4 id="树状数组的更新"><a href="#树状数组的更新" class="headerlink" title="树状数组的更新"></a>树状数组的更新</h4><p>&emsp;&emsp;树状数组的更新时$O(logn)$量级的，只需更新与更新元素有关的元素，比如我要更新<code>A[2]</code>,则需要更新<code>C[2]</code>,<code>C[4]</code>,<code>C[8]</code>；而更新<code>A[8]</code>，只需要更新<code>C[8]</code><br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">updata</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;c, <span class="keyword">int</span> i)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">while</span> (i &lt; c.<span class="built_in">size</span>())</span><br><span class="line">    &#123;</span><br><span class="line">        c[i]++;</span><br><span class="line">        i += low(i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="树状数组计算前缀和"><a href="#树状数组计算前缀和" class="headerlink" title="树状数组计算前缀和"></a>树状数组计算前缀和</h4><p>&emsp;&emsp;由于求区间和前缀和是一样的原理，所以本文在此处主要介绍前缀和的计算方式，由上文说的计算公式<code>sum[i] = C[i] + C[lowbit(i)] + ...</code>直至<code>lowbit(i)</code>等于0时终止算法。<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;c, <span class="keyword">int</span> i)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (i &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        sum += c[i];</span><br><span class="line">        i -= low(i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="树状数组的应用"><a href="#树状数组的应用" class="headerlink" title="树状数组的应用"></a>树状数组的应用</h3><p>&emsp;&emsp;<strong>Leetcode部分习题，后续应该还会有更新</strong></p>
<h4 id="计算右侧小于当前元素的个数"><a href="#计算右侧小于当前元素的个数" class="headerlink" title="计算右侧小于当前元素的个数"></a><a href="https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/" target="_blank" rel="noopener">计算右侧小于当前元素的个数</a></h4><p>&emsp;&emsp;给定一个整数数组 <code>nums</code>，按要求返回一个新数组 <code>counts</code>。数组 <code>counts</code> 有该性质： <code>counts[i]</code> 的值是  <code>nums[i]</code> 右侧小于 <code>nums[i]</code> 的元素的数量。</p>
<blockquote>
<p>输入: <code>[5,2,6,1]</code><br>输出: <code>[2,1,1,0]</code><br>解释:<br>5 的右侧有 2 个更小的元素 (2 和 1).<br>2 的右侧仅有 1 个更小的元素 (1).<br>6 的右侧有 1 个更小的元素 (1).<br>1 的右侧有 0 个更小的元素.</p>
</blockquote>
<p>&emsp;&emsp;上述习题就可以使用树状数组计算，由于数字的范围可能很大分布稀疏，所以对数组进行<strong>离散化</strong>，例如上述数组<code>[5,2,6,1]</code>将其映射为每个数在该数组排序好的排位，数组排序好为<code>[1,2,5,6]</code>，那么上述数组将被映射为<code>[3,2,4,1]</code>，当然在实际编码的过程中是使用<code>map</code>存储映射关系的。映射完成后，我们得到了每个数字对应的排位，就可以利用树状数组计算前缀和，快速得出每一个答案，将数组从右往左遍历，一遍计算前缀和一遍更新树状数组，比如我们在求<code>6</code>右边有多少个数小于它时，即<code>sum[5] = C[3] + C[2]</code>,然后更新<code>A[4]</code>即只需要更新<code>C[4]</code><br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">get_map</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; nums, <span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; &amp;ls)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; no_nums;</span><br><span class="line">        sort(nums.<span class="built_in">begin</span>(), nums.<span class="built_in">end</span>());</span><br><span class="line">        <span class="keyword">int</span> cur = nums[<span class="number">0</span>];</span><br><span class="line">        no_nums.push_back(cur);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.<span class="built_in">size</span>(); i++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] != cur)</span><br><span class="line">            &#123;</span><br><span class="line">                no_nums.push_back(nums[i]);</span><br><span class="line">                cur = nums[i];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; no_nums.<span class="built_in">size</span>(); i++)</span><br><span class="line">            ls[no_nums[i]] = i + <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">low</span><span class="params">(<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> x &amp; -x;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">updata</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;c, <span class="keyword">int</span> i)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (i &lt; c.<span class="built_in">size</span>())</span><br><span class="line">        &#123;</span><br><span class="line">            c[i]++;</span><br><span class="line">            i += low(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;c, <span class="keyword">int</span> i)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (i &gt; <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            sum += c[i];</span><br><span class="line">            i -= low(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">countSmaller</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;nums)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(!nums.<span class="built_in">size</span>())</span><br><span class="line">            <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">res</span><span class="params">(nums.<span class="built_in">size</span>(), <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; ls;</span><br><span class="line">        get_map(nums, ls);</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">c</span><span class="params">(ls.<span class="built_in">size</span>() + <span class="number">1</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = nums.<span class="built_in">size</span>() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> rank = ls[nums[i]];</span><br><span class="line">            res[i] = query(c, rank<span class="number">-1</span>);</span><br><span class="line">            updata(c, rank);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<p>&emsp;&emsp;<strong>本文为作者在网上查找资料后自己的理解，如有错误之处欢迎各位指正，有问题也欢迎来沟通</strong></p>
<blockquote>
<p>QQ:1612000416<br>邮箱：cz1612000416@163.com</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/Leetcode/" rel="tag"># Leetcode</a>
              <a href="/blog/tags/%E5%89%8D%E7%BC%80%E5%92%8C/" rel="tag"># 前缀和</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/06/22/Gitee-Hexo%E9%85%8D%E7%BD%AE%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E4%B8%AD%E7%9A%84%E5%9D%91/" rel="prev" title="Gitee+Hexo配置个人博客中的坑">
      <i class="fa fa-chevron-left"></i> Gitee+Hexo配置个人博客中的坑
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/07/24/%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E6%97%8B%E8%BD%AC%E9%97%AE%E9%A2%98/" rel="next" title="有序数组旋转问题">
      有序数组旋转问题 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#lowbit-函数的意义"><span class="nav-number">1.</span> <span class="nav-text">lowbit()函数的意义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#树状数组详解"><span class="nav-number">2.</span> <span class="nav-text">树状数组详解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#树状数组的更新"><span class="nav-number">2.1.</span> <span class="nav-text">树状数组的更新</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#树状数组计算前缀和"><span class="nav-number">2.2.</span> <span class="nav-text">树状数组计算前缀和</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#树状数组的应用"><span class="nav-number">3.</span> <span class="nav-text">树状数组的应用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#计算右侧小于当前元素的个数"><span class="nav-number">3.1.</span> <span class="nav-text">计算右侧小于当前元素的个数</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Cheng</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">4</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Cheng</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/muse.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
          load: ['[tex]/mhchem'],
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
          packages: {'[+]': ['mhchem']},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

</body>
</html>
